package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.BaseResult;
import com.txsj.scan.bean.EpcInfo;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.bean.PreBindBean;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.UrlInterface;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.MD5Util;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.ILabelView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Created by WangChuangJun on 2019/2/21
 * @Describe
 */
public class LabelPresenter extends BasePresenter<ILabelView> {
    public LabelPresenter(ILabelView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }

    /**
     * 扫描返回的条形码进行商品识别
     * @param data
     */
    public void getBarCodeHttp(String data) {
        Map<String, String> params = new HashMap<>();
        params.put("userid", PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,0)+"");
        params.put("token", PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN,""));
        params.put("brandid", PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,""));
        params.put("goodsnum", data);
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).getBarCodeGoods(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<GoodsInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseListResult<GoodsInfo> result) {
                        Trace.e("wcj+++++++"+result.getMsg());
                        if (result.isSuccess()){
                            if (null != result.getInfo() && null != result.getInfo() && result.getInfo().size() > 0){
                                view.onBarCodeLoadSuccess(result.getInfo().get(0));
                            }else {
                                view.onError("暂无该商品数据");
                            }
                        }else {
                            view.onError(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Trace.e("wcj+++++++"+e.getMessage());
                        view.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 扫描返回的二维码返回的标签
     * @param data
     */
    public void getQrCodeHttp(final String data) {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN,""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("code", data);
        params.put("type", "0");
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).getQrCodeHttp(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResult<EpcInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResult<EpcInfo> result) {
                        Trace.e("wcj+++++++"+result.getMsg());
                        if (result.isSuccess()){
                            if (null != result.getData()){
                                view.onQrCodeLoadSuccess(result.getData());
                            }else {
                                view.onError(result.getMsg());
                            }
                        }else {
                            view.onError(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Trace.e("wcj+++++++"+e.getMessage());
                        view.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    /**
     * 绑定商品及批次信息
     * @param id
     * @param goods_id
     * @param batch_id
     */
    public void bindGoodsBatch(long id, int goods_id, long batch_id) {
        Map<String,String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN,""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("batch_id", batch_id+"");
        params.put("goods_id", goods_id+"");
        params.put("id", id+"");
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).bindGoodsBatch(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResult result) {
                        if (result.isSuccess()){
                            view.onbindSuccess();
                        }else {
                            view.onError(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取已经预绑定的标签信息
     *
     * @param tag_id
     */
    public void getRFIDDetail(long tag_id) {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("type", "1");
        params.put("code", tag_id+"");
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).getRFIDDetail(RetrofitManager
                .getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResult<EpcInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResult<EpcInfo> result) {
                        if (result.isSuccess() && null != result.getData()) {
                            view.onGetRFIDSuccess(result.getData());
                        } else {
                            view.onError(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 取消绑定标签
     * @param id
     */
    public void unBindBatch(long id) {
        Map<String,String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN,""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("id", id+"");
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).unBindGoodsBatch(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResult result) {
                        if (result.isSuccess()){
                            view.onUnbindSuccess();
                        }else {
                            view.onError(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 预绑定标签
     * @param tag_id
     */
    public void preBindLabel(long tag_id) {
        Map<String,String> params = new HashMap<>();
        String timeStamp = System.currentTimeMillis() / 1000 + "";
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN,""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("tag_id", tag_id+"");
        params.put("timeStamp", timeStamp);//加时间戳参数
//        params.put("timestamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class)
                .preBindLabel(RetrofitManager.getUrl(UrlInterface.preBindLabel,params),
                        RetrofitManager.getHeader(),
                        RetrofitManager.getNewParams(params))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResult<PreBindBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResult<PreBindBean> result) {
                        if (result.isSuccess()){
                            view.onPrebindSuccess(result.getData());
                        }else {
                            view.onPrebindError(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
